#include <59header.h>

/* Usage: ./09_my_ls_simp dirname  */
int main(int argc, char *argv[]){
    ARGS_CHECK(argc, 2);

    DIR *dirp = opendir(argv[1]);
    ERROR_CHECK(dirp, NULL, "opendir");
    // 推荐办法一：切换工作目录
    //int ret = chdir(argv[1]);
    //ERROR_CHECK(ret, -1, "chdir");

    struct dirent* direntp;
    while((direntp = readdir(dirp)) != NULL){
        // 方法二：拼接文件的绝对路径名然后传参给stat函数
        char path[1024] = {0};
        //strcpy(path, argv[1]);
        //strcat(path, "/");
        //strcat(path, direntp->d_name);
        sprintf(path, "%s%s%s", argv[1], "/",
                direntp->d_name);
        struct stat stat_buf;
        int ret = stat(path, &stat_buf);
        ERROR_CHECK(ret, -1, "stat");
        
        // stat_buf.st_mtim.tv_sec; 时间戳的秒数
        // stat_buf.st_mtime; 等价写法，也是当前时间戳秒数
        printf("%o %lu %u %u %lu %lu %s\n",
                stat_buf.st_mode,
                stat_buf.st_nlink,
                stat_buf.st_uid,
                stat_buf.st_gid,
                stat_buf.st_size,
                stat_buf.st_mtim.tv_sec,
                direntp->d_name
               );
    }
    closedir(dirp);
    return 0;
}   

